package com.zryc.shop.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Admin;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.entity.Role;
import com.zryc.shop.ums.mapper.AdminMapper;
import com.zryc.shop.ums.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {


    /**
     * 更新管理员状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Object updateAdminStatus(Long id, Integer status) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        update(updateWrapper);
        return null;
    }

    /**
     * 更新管理员信息
     *
     * @param id
     * @param admin
     * @return
     */
    @Override
    public Object updateAdmin(Long id, Admin admin) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(admin,updateWrapper);
        return null;
    }

    /**
     * 修改管理员密码
     *
     * @param id
     * @param oldPass
     * @param newPass
     * @return
     */
    @Override
    public Object updateAdminPassword(Long id, String oldPass, String newPass) {
        Admin admin = baseMapper.selectById(id);
        if (admin == null) {
            return Result.error(400000, "用户不存在");
        }

        if (oldPass.equals(newPass)) {
            return Result.error(400001, "新密码与旧密码相同");
        }
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("password", BCrypt.hashpw(newPass, BCrypt.gensalt()));
        return update(updateWrapper);
    }

    /**
     * 分页查询admin
     *
     * @param keyword  关键字 用户名/昵称
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Admin> list(String keyword, Integer pageNum, Integer pageSize) {
        Page<Admin> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("username", keyword).or().like("nick_name", keyword);
        }
        return page(page, wrapper);
    }


    /**
     * 管理员登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Admin login(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Admin admin = baseMapper.selectOne(wrapper);
        if (admin != null && admin.getStatus() == 1 && BCrypt.checkpw(password, admin.getPassword())) {
                UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id",admin.getId());
                updateWrapper.set("login_time",LocalDateTime.now());
                update(updateWrapper);
                return admin;
        }
        return null;
    }

    /**
     * 根据用户id查询所有菜单列表
     *
     * @param adminId
     * @return
     */
    @Override
    public List<Menu> getAllMenuByAdminId(Long adminId) {
//        List<Role> roles = roleService.getRoleByAdminId(adminId);
//        List<Menu> menus=new ArrayList<>();
//        for(Role role:roles){
//            for (Menu menu : menuService.getMenuByRoleId(role.getId())) {
//                menus.add(menu);
//            }
//        }
        List<Menu> menus = this.getBaseMapper().getAllMenuByAdminId(adminId);
        return menus;
    }

    /**
     * 通过id 删除管理员
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Object deleteAdminById(Long id) {
        baseMapper.deleteById(id);
        baseMapper.deleteAdminFromMiddle(id);
        return "删除成功";
    }

    /**
     * 登出
     *
     * @param session
     * @return
     */
    @Override
    public Object logout(HttpSession session) {
        session.invalidate();
        return "退出成功";
    }


}
